﻿package 
{
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import fl.controls.Slider;
	import fl.events.SliderEvent;


	public class CommandPanel extends Sprite
	{
		private var initX:int = 0;
		private var initY:int = 0;
		private var panelWidth = 124;
		private var panelHeight = 540;
		private var theAlpha:Number = 0.2;
		private var lineColor:uint = 0x0;
		private var fillColor:uint = 0xffffff;
		private var txtColor:uint = 0x0;
		private var txtArray:Array = new Array("风速","风向","气压","雨量","温度","湿度","照度","土壤温度","土壤湿度","叶表温度","叶表湿度");
		private var TFArray:Array = new Array();
		private var txtPosX:Array = new Array(2,2,2,2,2,2,2,2,2,2,2);
		//private var txtPosX:Array = new Array(24,24,24,24,24,24,24,0,0,0,0);
		private var sliderMinArray:Array = new Array(0,0,0,0,0,0,0,0,0,0,0);
		private var sliderMaxArray:Array = new Array(6,360,50,100,50,100,1000,75,255,75,255);
		private var sliderValueArray:Array = new Array(1,0,29,70,32,70,255,73,255,73,255);
		private var sliderValueNewArray:Array = new Array(1,0,29,70,32,70,255,73,255,73,255);
		private var sliderArray:Array = new Array();
		private var txtPosYFirst:int = 50;
		private var posYAdding:int = 40;
		private var sliderPosX:int = 6;
		private var sliderPosYFirst:int = 70;
		private var sliderLength:int = 110;
		private var DifferRatio:int = 0;
		
		public function getDifferRatio():int
		{
			return DifferRatio;
		}
		
	    private function caculateDifferRatio():void
		{
			DifferRatio = 0;
			for (var i:int =0; i< sliderMinArray.length;i++)
			{
				if (i==1) continue;
				var dLen:int = sliderValueNewArray[i]-sliderValueArray[i];
				if (dLen<0) dLen = -dLen;
				var div:Number = sliderMaxArray[i] - sliderMinArray[i];
				if (div ==0) div=1;
				DifferRatio+=dLen*51/div;
			}
			if (DifferRatio>50) DifferRatio = 50;
		}
		public function CommandPanel(posX:int,posY:int):void
		{
			initX = posX;
			initY = posY;
			doDrawRect();
			doDrawText();
			doDrawSlider();
		}
		
		private function doDrawRect():void
		{
			var child:Shape = new Shape();
			child.graphics.beginFill(fillColor,theAlpha);
			child.graphics.lineStyle(1, lineColor,theAlpha);
			child.graphics.drawRect(initX, initY, panelWidth, panelHeight);
			child.graphics.endFill();
			addChild(child);

		}

		private function doDrawText():void
		{
			var theText:TextField;
			for (var i:int = 0; i<txtArray.length; i++)
			{
				theText = new TextField();
				theText.text = txtArray[i]+ "  " +sliderValueArray[i];
				theText.x = initX + txtPosX[i];
				theText.y = initY + txtPosYFirst + posYAdding * i;
				theText.textColor = txtColor;
				addChild(theText);
				TFArray.push(theText);
			}
		}

		private function doDrawSlider():void
		{
			var theSlider:Slider;
			for (var i:int = 0; i<txtArray.length; i++)
			{
				theSlider = new Slider();
				theSlider.minimum = sliderMinArray[i];
				theSlider.maximum = sliderMaxArray[i];
				theSlider.value = sliderValueArray[i];
				theSlider.width = sliderLength;
				theSlider.x = initX + sliderPosX;
				theSlider.y = initY + sliderPosYFirst + posYAdding * i;
				sliderArray.push(theSlider);
				addChild(theSlider);
			}
		}
		public function getSlider(index:int):Slider
		{
			return sliderArray[index];
		}
		
		public function setLabelValue(index:int,value:int):void
		{
			TFArray[index].text = txtArray[index] + "  " +value;
			sliderValueNewArray[index]=value;
			caculateDifferRatio();
		}

	}
}